Trò chơi game bắn súng đơn giản trong UNITY Engine
23.607 lượt xem;
1 using UnityEngine;
2 using System.Collections;
3
4 public class PlayerBullet : MonoBehaviour
5 {
6 Rigidbody rb;
7 public float speed;
8
9 void Awake ()
10 {
11 rb = GetComponent<Rigidbody>();
12 }
13
14 public void ShootMe(Vector3 startPos)
15 {
16 PlayerControl.maxBulletsOnScreen--;
17 transform.position = startPos;
18 rb.velocity = new Vector3(0,0,1 * speed);
19 }
20
21 void OnTriggerExit(Collider col)
22 {
23 if (col.tag == "SpawnBorders")
24 {
25 BulletIsOff();
26 }
27 }
28
29 void OnTriggerEnter(Collider col)
30 {
31 if (col.tag == "Building")
32 {
33 BulletIsOff();
34 }
35 else if (col.tag == "Enemy")
36 {
37 col.GetComponent<IInterractWithBullet>().GetDamage();
38 BulletIsOff();
39 }
40 }
41
42 void BulletIsOff()
43 {
44 PlayerControl.maxBulletsOnScreen++;
45 rb.velocity = Vector3.zero;
46 gameObject.SetActive(false);
47 }
48 }
2 using System.Collections;
3
4 public class PlayerBullet : MonoBehaviour
5 {
6 Rigidbody rb;
7 public float speed;
8
9 void Awake ()
10 {
11 rb = GetComponent<Rigidbody>();
12 }
13
14 public void ShootMe(Vector3 startPos)
15 {
16 PlayerControl.maxBulletsOnScreen--;
17 transform.position = startPos;
18 rb.velocity = new Vector3(0,0,1 * speed);
19 }
20
21 void OnTriggerExit(Collider col)
22 {
23 if (col.tag == "SpawnBorders")
24 {
25 BulletIsOff();
26 }
27 }
28
29 void OnTriggerEnter(Collider col)
30 {
31 if (col.tag == "Building")
32 {
33 BulletIsOff();
34 }
35 else if (col.tag == "Enemy")
36 {
37 col.GetComponent<IInterractWithBullet>().GetDamage();
38 BulletIsOff();
39 }
40 }
41
42 void BulletIsOff()
43 {
44 PlayerControl.maxBulletsOnScreen++;
45 rb.velocity = Vector3.zero;
46 gameObject.SetActive(false);
47 }
48 }